/**
 * Created by xmh on 2017/4/28.
 */

import {Injectable} from '@angular/core';

import {HttpService} from './httpService';
import {EncryptService} from './encryptService';

@Injectable()
export class IntfService {


  constructor(private httpService: HttpService,
              private encryptService: EncryptService) {
    //console.log('Hello HttpService Provider');
  }


  //json方式处理post调用接口, encryptTag是否加密
  postJsonReq(url, param, encryptTag: boolean){
      if(encryptTag){
        return this.handerPostJsonReqEccrypt(url, param); //返回promise
      }
      else{
        return this.handerPostJsonReq(url, param); //返回promise
      }
  }

  //form方式处理post调用接口, encryptTag是否加密
  postFormReq(url, param, encryptTag: boolean){
    if(encryptTag){
      return this.handerPostFormReqEccrypt(url, param); //返回promise
    }
    else{
      return this.handerPostFormReq(url, param); //返回promise
    }
  }

  //处理get调用接口, encryptTag是否加密
  getReq(url, param, encryptTag: boolean){
    if(encryptTag){
      return this.handerGetReqEccrypt(url, param); //返回promise
    }
    else{
      return this.handerGetReq(url, param); //返回promise
    }
  }

  //json方式处理加密的post请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerPostJsonReqEccrypt(url: string, param: any){

    let reqJson = JSON.stringify(param);
    let enResult = this.encryptService.encryptByDES(reqJson); //加密参数
    let encryptParam = {'reqParam': enResult}; //重新拼装请求参数

    return this.httpService.postJson(url, encryptParam).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文

      //-1的情况是参数校验失败，后台服务端压根不加密
      if(data.respCode == '-1'){
        return data;
      }

      //解密，只是返回里面的data部分
      let deResult = this.encryptService.decryptByDES(data.respInfo);
      let deResultJson: any = JSON.parse(deResult); //返回json对象给页面类
      return deResultJson;

    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }

  //json方式处理不加密的post请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerPostJsonReq(url: string, param: any){
    return this.httpService.postJson(url, param).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文
      return data;  //返回json对象给页面类
    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }

  //form提交方式处理加密的post请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerPostFormReqEccrypt(url: string, param: any){
    let reqJson = JSON.stringify(param);
    let enResult = this.encryptService.encryptByDES(reqJson); //加密参数
    let encryptParam = {'reqParam': enResult}; //重新拼装请求参数

    return this.httpService.postForm(url, encryptParam).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文

      //-1的情况是参数校验失败，后台服务端压根不加密
      if(data.respCode == '-1'){
        return data;
      }

      //解密，只是返回里面的data部分
      let deResult = this.encryptService.decryptByDES(data.respInfo);
      let deResultJson: any = JSON.parse(deResult); //返回json对象给页面类
      return deResultJson;

    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }

  //form提交方式处理不加密的post请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerPostFormReq(url: string, param: any){
    return this.httpService.postForm(url, param).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文
      return data;
    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }

  //处理加密的get请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerGetReqEccrypt(url: string, param?: any){
    let reqJson = JSON.stringify(param);
    let enResult = this.encryptService.encryptByDES(reqJson); //加密参数
    let encryptParam = {'reqParam': enResult}; //重新拼装请求参数

    return this.httpService.get(url, encryptParam).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文
      //-1的情况是参数校验失败，后台服务端压根不加密
      if(data.respCode == '-1'){
        return data;
      }

      //解密，只是返回里面的data部分
      let deResult = this.encryptService.decryptByDES(data.respInfo);
      return deResult;

    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }

  //处理不加密的get请求，明文param进来--加密param--请求--密文resp--明文resp返回
  private handerGetReq(url: string, param: any){
    return this.httpService.get(url, param).then((data: any) => {
      //let respJson = JSON.stringify(data.respInfo);//data是对象转字符串,respInfo是密文
      return data;
    }).catch((error: any) => {
      console.log('req error:' + JSON.stringify(error));
      throw error; //抛出异常，前端才能catch
    });
  }
}
